package com.examinantsdk.base

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toolbar
import androidx.activity.ComponentActivity
import com.examinantsdk.R
import com.examinantsdk.base.view.IBaseView
import com.examinantsdk.base.view.LoadingDialog
import com.examinantsdk.base.view.stateview.StateView
import com.gyf.immersionbar.ktx.immersionBar

/**
 * Create by liuwei on 2021/7/20.
 *
 */
abstract class CetBaseActivity : ComponentActivity(), IBaseView {
    private var dataLoaded = false
    var mStateView: StateView? = null
    var toolBar: View? = null
    var loadingDialog: LoadingDialog? = null
    var autoShowLoading = true
    var antButton: ImageView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val viewRoot = initView()
        setContentView(viewRoot)
        BaseAppManager.getInstance().addActivity(this)
        loadingDialog = LoadingDialog(this)
        toolBar = findViewById(R.id.toolBar)
        if (toolBar is Toolbar) {
            (toolBar as Toolbar).setNavigationOnClickListener { finish() }
        }
        val viewTarget: View? = bindStateTarget()
        if (viewTarget != null) {
            mStateView = StateView.inject(viewTarget)
//            mStateView?.setTheme(isDarkTheme())
//            if (this is SearchWordActivity) {
//                mStateView?.setLoadingResource(R.layout.vote_list_loading)
//            }
            mStateView?.setOnButtonClickListen { viewType: Int ->
                if (viewType == StateView.RETRY) {
                    onErrorClick()
                } else if (viewType == StateView.EMPTY) {
                    onEmptyClick()
                }
            }
        }
        initImmersionBar()
        if (autoShowLoading) {
            showLoading()
        }

//        addAiFloatButton(viewRoot)
    }

    /**
     * 添加小蚁同学入口
     */
    private fun addAiFloatButton(viewRoot: View) {
//        if (
//            this !is HomeActivity
//            && !isCet()
//            && !isGetEnglish()
//            && !isZSBEnglish()
//        ) {
//            return
//        }
//        when (this) {
//            is AiChatActivity,
//            is PersonalPlanActivity,
//            is ChooseCetActivity,
//            is ChooseGetCoursesActivity,
//            is SettingUI,
//            is ThemeSetActivity,
//            is FeedBackDetailActivity,
//            is EditUserUI,
//            is TCVideoCutActivity,
//            is UnregisterActivity,
//            is TBSWebViewActivity,
//            is UGCDraftListActivity,
//            is MembersIntegralCenterActivity,
//            is PlayVideosActivity
//            -> return
//        }
//
//        try {
//            antButton = DragFloatActionButton(this)
//            antButton?.setBackgroundResource(R.drawable.anim_ai_ant_right)
//            (viewRoot as ViewGroup).addView(antButton, 75.dp2px(), 88.dp2px())
//
//            antButton?.setOnImageClickListener(
//                {
//                    startActivity(Intent(this, AiChatActivity::class.java))
//                },
//                (-20).dp2px()
//            )
//
//            val aiY = SPUtils.getInstance().getFloat("ai_y")
//            val aiDire = SPUtils.getInstance().getInt("ai_direction", 2)
//            antButton?.post {
//                if (aiY > 0) {
//                    antButton?.y = aiY
//                } else {
//                    antButton?.y = screenHeight() * 0.7f
//                }
//                if (aiDire == 2) {
//                    antButton?.x = (screenWidth() - antButton!!.width + 20.dp2px()).toFloat()
//                } else {
//                    antButton?.x = (-20).dp2px().toFloat()
//                }
//                antButton?.frameAnim(
//                    R.mipmap.ant,
//                    if (aiDire == 1) R.drawable.anim_ai_ant_left else R.drawable.anim_ai_ant_right,
//                    true
//                )
//                antButton?.visibleAntButton()
//            }
//
//        } catch (e: Exception) {
//            e.printStackTrace()
//        }
    }

    open fun initImmersionBar() {
        immersionBar {
            transparentStatusBar()
            statusBarDarkFont(true)
            navigationBarColor(R.color.white)
            navigationBarDarkIcon(true)
            titleBar(toolBar)
        }
    }

    open fun setFloatButtonParent(): ViewGroup? {
        return null
    }
    /**
     * 绑定状态视图
     * @return
     */
    open fun bindStateTarget(): View? {
        return null
    }

    open fun onEmptyClick() {}
    open fun onErrorClick() {}

    override fun showEmpty(msg: String?) {
        mStateView?.showEmpty(msg)
        dataLoaded = true
    }

    open fun showEmpty(resId: Int, msg: String?) {
        mStateView?.showEmpty(resId, msg)
        dataLoaded = true
    }

    open fun showEmpty(msg: String, showBtn: Boolean) {
        mStateView?.showEmpty(msg, showBtn)
        dataLoaded = true
    }


    override fun showError(msg: String?) {
//        if (!dataLoaded) {
        mStateView?.showRetry()
//        }
    }

    override fun showLoading() {
        mStateView?.showLoading()
    }

    override fun showContent() {
        mStateView?.showContent()
        dataLoaded = true
    }

    abstract fun initView(): View

    override fun showToast(msg: String?) {
        if (null != msg) {
            ToastUtil.showToast(this, msg)
        }
    }

    override fun onDestroy() {
        loadingDialog?.dismiss()
        loadingDialog = null
        mStateView?.release()
        BaseAppManager.getInstance().removeActivity(this)
        super.onDestroy()
    }

    private var exitTime: Long = 0
    open fun fastClick(time: Int = 1500): Boolean {
        exitTime = if (System.currentTimeMillis() - exitTime > time) {
            System.currentTimeMillis()
        } else {
            return true
        }
        return false
    }

}